<?php
class Gamer_model extends Base_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function getGamertel($start,$end)
    {
        $res = $this->db->select('g.*,u.telNum')
         ->from('mod_user as u')
         ->join('mod_gamer_info as g','g.uid=u.uid','left')
         ->where(array('u.utype'=>'gamer'))
         ->where('g.created >'.$start.' AND g.created <'.$end)
         ->get()->result_array();
//        foreach ($res as &$row){
//            $row['created']=date('Y-m-d H:i:s',$row['created']);
//        }
//        array_unshift($res,array('姓名','手机','申请日期'));
        return $res;

    }

    public function getAllGamerinfo($start,$end)
    {
//        $data = $this->db->select('*')->from('mod_gamer_info')->where('created >'.$start.' AND created <'.$end)->get()->result_array();
        $data = $this->getGamertel($start,$end);
        $livecity = $this->db->select('g.uid,c.city')->from('mod_gamer_info as g')->join('mod_city as c','c.cityID=g.livecity','left')->where('g.created >'.$start.' AND g.created <'.$end)->get()->result_array();
        $datalc = array();
        $livearea = $this->db->select('g.uid,a.area')->from('mod_gamer_info as g')->join('mod_area as a','a.areaID=g.livearea','left')->where('g.created >'.$start.' AND g.created <'.$end)->get()->result_array();
        $datala = array();
        $jobcity = $this->db->select('g.uid,c.city')->from('mod_gamer_info as g')->join('mod_city as c','c.cityID=g.jobcity','left')->where('g.created >'.$start.' AND g.created <'.$end)->get()->result_array();
        $datajc = array();
        $jobarea = $this->db->select('g.uid,a.area')->from('mod_gamer_info as g')->join('mod_area as a','a.areaID=g.jobarea','left')->where('g.created >'.$start.' AND g.created <'.$end)->get()->result_array();
        $dataja = array();
        foreach ($livecity as $row){
            $datalc[$row['uid']] = $row['city']; 
        }
        foreach ($livearea as $row){
            $datala[$row['uid']] = $row['area'];
        }
        foreach ($jobcity as $row){
            $datajc[$row['uid']] = $row['city'];
        }
        foreach ($jobarea as $row){
            $dataja[$row['uid']] = $row['area'];
        }
        $res = array();
        foreach ($data as $row){
            $res[$row['uid']]['created']=date('Y-m-d H:i:s',$row['created']);
            $res[$row['uid']]['userName'] = $row['userName'];
            $res[$row['uid']]['telNum'] = $row['telNum'];
            $res[$row['uid']]['userGender'] = $row['userGender']=='m'?'男':'女';
            $res[$row['uid']]['marriage'] = $row['marriage']==1?'已婚':'单身';
            $res[$row['uid']]['childage'] = $row['childage'];
            switch ((int)$row['eduLevel']){
                case 1:
                    $res[$row['uid']]['eduLevel']='高中及以下';
                    break;
                case 2:
                    $res[$row['uid']]['eduLevel']='本科';
                    break;
                case 3:
                    $res[$row['uid']]['eduLevel']='研究生';
                    break;
                case 4:
                    $res[$row['uid']]['eduLevel']='博士';
                    break;
                default:
                    $res[$row['uid']]['eduLevel']='';
            }
            $res[$row['uid']]['career']=$row['career'];
            switch ((int)$row['careerstatus']){
                case 1:
                    $res[$row['uid']]['careerstatus']='全职';
                    break;
                case 2:
                    $res[$row['uid']]['careerstatus']='兼职';
                    break;
                case 3:
                    $res[$row['uid']]['careerstatus']='离职';
                    break;
                case 4:
                    $res[$row['uid']]['careerstatus']='退休';
                    break;
                default:
                    $res[$row['uid']]['careerstatus']='';
            }
            $res[$row['uid']]['livecity']=$datalc[$row['uid']];
            $res[$row['uid']]['livearea']=$datala[$row['uid']];
            $res[$row['uid']]['jobcity']=$datajc[$row['uid']];
            $res[$row['uid']]['jobarea']=$dataja[$row['uid']];
            $res[$row['uid']]['cert'] = $row['cert'];
            $res[$row['uid']]['hobby'] = $row['hobby'];
            foreach (igbinary_unserialize($row['freetime']) as $v){
                $char1 = substr($v,0,1);
                $char2 = substr($v,1,1);
                switch ($char1){
                    case '1':
                        $res[$row['uid']]['freetime'].='周一';
                        break;
                    case '2':
                        $res[$row['uid']]['freetime'].='周二';
                        break;
                    case '3':
                        $res[$row['uid']]['freetime'].='周三';
                        break;
                    case '4':
                        $res[$row['uid']]['freetime'].='周四';
                        break;
                    case '5':
                        $res[$row['uid']]['freetime'].='周五';
                        break;
                    case '6':
                        $res[$row['uid']]['freetime'].='周六';
                        break;
                    case '7':
                        $res[$row['uid']]['freetime'].='周日';
                        break;
                    default:
                        $res[$row['uid']]['freetime'].='';
                }
                switch ($char2){
                    case '1':
                        $res[$row['uid']]['freetime'].='上午; ';
                        break;
                    case '2':
                        $res[$row['uid']]['freetime'].='下午; ';
                        break;
                    case '3':
                        $res[$row['uid']]['freetime'].='晚上';
                        break;
                    default:
                        $res[$row['uid']]['freetime'].='';
                }
            }
            $res[$row['uid']]['reason']=$row['reason'];
            $res[$row['uid']]['hope']=$row['hope'];
            $res[$row['uid']]['experience']=$row['experience'];

        }
        $res = array_values($res);
        array_unshift($res, array('申请日期','姓名','电话','性别','婚姻状况','孩子年龄','学历','职业','职业状态','居住城市','居住区域','工作城市','工作区域','技能','兴趣','空余时间','报名原因','期待','经历'));
        return $res;
    }
}